package com.klouda.platakybeta.db;

import com.klouda.platakybeta.PlatakyActivity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class PlatakyDbHandler {

  // If you change the database schema, you must increment the database version.
  public static final int DATABASE_VERSION = 1;
  public static final String DATABASE_NAME = "plataky.db";

  public PlatakyDbHandler(Context p_context) {
    m_context = p_context;
  }
  
  public PlatakyDbHandler open() {
    m_dbHelper = new PlatakyDbHelper(m_context);
    m_db = m_dbHelper.getWritableDatabase();
    return this;
  }

  public void close() {
    if (m_dbHelper != null) {
      m_dbHelper.close();
    }
  }
  
  public boolean createLek(String p_nazev, float p_cena) {
    ContentValues l_cvLek = new ContentValues();
    l_cvLek.put(Tables.TLeky.COLUMN_NAME_NAZEV, p_nazev);
    l_cvLek.put(Tables.TLeky.COLUMN_NAME_CENA, p_cena);
    try {
      open();
      return (m_db.insert(Tables.TLeky.TABLE_NAME, null, l_cvLek) < 0 ? false : true);      
    } catch (Exception l_ex) {
      doHandleException(l_ex);
      return false;
    } finally {
       close();
    }
  }

  public boolean deleteLek(int p_id) {
    try {
      open();
      m_db.delete(Tables.TLeky.TABLE_NAME, Tables.TLeky.COLUMN_NAME_ID + "=?", new String[]{String.valueOf(p_id)});
    } catch (Exception l_ex) {
      doHandleException(l_ex);
      return false;
    } finally {
       close();
    }
    return true;
  }
  
  public Cursor getAllLeky() {
    return m_db.query(Tables.TLeky.TABLE_NAME, new String[] {
        Tables.TLeky.COLUMN_NAME_ID, Tables.TLeky.COLUMN_NAME_NAZEV,
        Tables.TLeky.COLUMN_NAME_CENA }, Tables.TLeky.COLUMN_NAME_SMAZANO
        + "=?", new String[] { String.valueOf(0) }, null, null,
        Tables.TLeky.COLUMN_NAME_NAZEV);
  }
  
  //!td predelat exception handling? udelat neco jako hlavni logger, jde logovat do fajlu?
  private void doHandleException(Exception l_ex) {
    Log.e(PlatakyActivity.ERROR_TAG, "Error while working with database", l_ex);
  }  
  
  private final Context m_context;
  private PlatakyDbHelper m_dbHelper = null;// new PlatakyDbHandler(this);
  private SQLiteDatabase m_db = null;//l_dbHelper.getWritableDatabase();  
  
  
  private static final String TEXT_TYPE = " TEXT";
  private static final String REAL_TYPE = " REAL";
  private static final String INTEGER_TYPE = " INTEGER";
  private static final String COMMA_SEP = ",";
  private static final String NOT_NULL = " NOT NULL";
  private static final String AUTOINCREMENT = " AUTOINCREMENT";
  private static final String PRIMARY_KEY = " PRIMARY KEY";
  private static final String DEFAULT = " DEFAULT ";

  private static final String SQL_CREATE_TABLES = "CREATE TABLE "
      + Tables.TLeky.TABLE_NAME + " (" + Tables.TLeky.COLUMN_NAME_ID
      + INTEGER_TYPE + PRIMARY_KEY + AUTOINCREMENT + COMMA_SEP
      + Tables.TLeky.COLUMN_NAME_NAZEV + TEXT_TYPE + NOT_NULL + COMMA_SEP
      + Tables.TLeky.COLUMN_NAME_CENA + REAL_TYPE + NOT_NULL + COMMA_SEP
      + Tables.TLeky.COLUMN_NAME_SMAZANO + INTEGER_TYPE + NOT_NULL + DEFAULT + "0" +  " )";

  private static final String SQL_DELETE_TABLES = "DROP TABLE IF EXISTS "
      + Tables.TLeky.TABLE_NAME;

  private static class PlatakyDbHelper extends SQLiteOpenHelper {
    public PlatakyDbHelper(Context context) {
      // ? !td ulozit context do membra?
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase p_db) {
      p_db.execSQL(SQL_CREATE_TABLES);

    }

    @Override
    public void onUpgrade(SQLiteDatabase p_db, int arg1, int arg2) {
      p_db.execSQL(SQL_DELETE_TABLES);
      onCreate(p_db);
    }
  }
  
  
}
